package com.lanlian.uikit;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by huxiaobo on 16/5/23.
 * 底部弹出的列表对话框
 */
public class CustomBottomSheetDialog extends Dialog implements View.OnClickListener {

    private ListView listView;
    private TextView tvCancel;
    private TextView tvTitle;
    private String title;
    private Context context;
    private int cancelColor;
    private OnItemOnClickListener onItemOnClickListener;
    //定义弹窗子类项列表
    private ArrayList<ActionItem> mActionItems = new ArrayList<>();

    public CustomBottomSheetDialog(Context context) {
        this(context, "");
    }

    public CustomBottomSheetDialog(Context context, String title) {
        super(context, R.style.customDialog);
        this.context = context;
        this.title = title;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_custom_bottom_sheet);

        ViewGroup.LayoutParams params = getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        listView = findViewById(R.id.list_view);
        tvCancel = findViewById(R.id.tv_cancel);
        tvTitle = findViewById(R.id.tv_title);
        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
        } else {
            tvTitle.setText(title);
            tvTitle.setVisibility(View.VISIBLE);
        }
        if (cancelColor != 0) {
            tvCancel.setTextColor(context.getResources().getColor(cancelColor));
        }
        tvCancel.setOnClickListener(this);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                onItemOnClickListener.onItemClick(mActionItems.get(position), position);
                dismiss();
            }
        });

        setShareAdapter();
        if (listView.getAdapter().getCount() > 5) {
            resetListViewHeight(listView);
        }
    }

    public void resetListViewHeight(ListView lv) {
        ListAdapter listAdapter = lv.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0; i < 5; i++) {
            View listItem = listAdapter.getView(i, null, lv);
            listItem.measure(0, 0); //计算子项view的宽高
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = lv.getLayoutParams();
        params.height = totalHeight + (lv.getDividerHeight() * (listAdapter.getCount() - 1));
        lv.setLayoutParams(params);
    }

    public void showDialog() {
        Window w = getWindow();
        w.setWindowAnimations(R.style.animFromBottom);
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        onWindowAttributesChanged(lp);
        setCanceledOnTouchOutside(true);
        show();
    }

    /**
     * 添加子类项
     */
    public void addAction(ActionItem action) {
        if (action != null) {
            mActionItems.add(action);
        }
    }

    private void setShareAdapter() {
        listView.setAdapter(new BaseAdapter() {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView textView = new TextView(context);
                ActionItem item = mActionItems.get(position);
                if (item.color != 0) {
                    textView.setTextColor(context.getResources().getColor(item.color));
                } else {
                    textView.setTextColor(context.getResources().getColor(R.color.color_K4));
                }
                textView.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
                textView.setHeight(context.getResources().getDimensionPixelSize(R.dimen.value_50dp));
                textView.setTextSize(20);
                textView.setText(item.mTitle);
                textView.setGravity(Gravity.CENTER);
                if (item.mDrawable != null) {
                    textView.setCompoundDrawablePadding(context.getResources().getDimensionPixelSize(R.dimen.value_8dp));
                    textView.setCompoundDrawablesWithIntrinsicBounds(item.mDrawable, null, null, null);
                } else {
                    textView.setCompoundDrawablePadding(0);
                    textView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
                }
                return textView;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public Object getItem(int position) {
                return mActionItems.get(position);
            }

            @Override
            public int getCount() {
                return mActionItems.size();
            }
        });
    }

    public void setCancelColor(int color) {
        this.cancelColor = color;
    }

    public void setOnItemClickListener(OnItemOnClickListener onItemOnClickListener) {
        this.onItemOnClickListener = onItemOnClickListener;
    }

    public interface OnItemOnClickListener {
        void onItemClick(ActionItem item, int position);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_cancel) {
            dismiss();
        }
    }

}
